package com.unione.map.view.act

import android.content.Intent
import android.os.Bundle
import android.util.Log
import com.google.gson.Gson
import com.google.gson.JsonObject
import com.unione.map.RobotMapApplication
import com.unione.map.constant.AppConstant
import com.unione.map.databinding.ActivityMainBinding
import com.unione.map.mmkv.AppConfigManager
import com.unione.map.view.fragment.CreateMapStep1Fragment
import com.unione.unione_base.view.AppBaseActivity
import com.unione.unione_network.AGVSocketManager
import com.unione.unione_network.tcp_socket.bean.TargetInfo
import com.unione.unione_network.tcp_socket.bean.TcpMsg
import com.unione.unione_network.tcp_socket.SpecifiedStickPackageHelper
import com.unione.unione_network.tcp_socket.TcpClient
import com.unione.unione_network.tcp_socket.TcpClientListener
import com.unione.unione_network.tcp_socket.TcpConnConfig
import com.unione.unione_network.tcp_socket.bean.AGVCmd
import com.unione.unione_network.tcp_socket.bean.BaseAGVMsg
import com.unione.unione_protocol.robot_auto.bean.RobotInfo


class MainActivity : AppBaseActivity(), TcpClientListener {

    private lateinit var binding: ActivityMainBinding
    var mRobotInfo: RobotInfo? = null


    companion object {
        const val TAG = "MainActivity"
    }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        val rootView = binding.root
        setContentView(rootView)
        //   checkLogin()
        initViewModelListener()
        testAGVTcp()
    }


    private fun checkLogin() {
        val userInfo = AppConfigManager.getUserInfo()
        if (userInfo == null) {
            startActivity(Intent(this, LoginActivity::class.java))
            finish()
        }
    }

    private fun testAGVTcp() {
        val tcpClient = AGVSocketManager.getTcpClient(
            TargetInfo(AppConstant.robot_ip, AppConstant.robot_port),
            TcpConnConfig().apply {

            }
        )
        tcpClient.addTcpClientListener(this)
        tcpClient.connect()
    }


    private fun initViewModelListener() {
        RobotMapApplication.appViewModel.liveDataRobotState.observe(this) { robotInfo ->
            try {
                mRobotInfo = robotInfo
            } catch (e: Exception) {
                Log.e(CreateMapStep1Fragment.TAG, e.localizedMessage.toString())
            }

        }
    }

    override fun onConnected(client: TcpClient) {

    }

    override fun onSendData(client: TcpClient, tcpMsg: TcpMsg) {

    }

    override fun onDisconnected(client: TcpClient, msg: String, e: Exception) {

    }

    override fun onReceive(client: TcpClient, tcpMsg: TcpMsg) {
        val baseAGVMsgLBaseAGVMsg = Gson().fromJson(tcpMsg.sourceDataString, BaseAGVMsg::class.java)
        val currentTime=System.currentTimeMillis()
        val timeInterval=currentTime-tcpMsg.time
        when (baseAGVMsgLBaseAGVMsg.cmd) {
            AGVCmd.map.name -> {
                Log.d(TAG, AGVCmd.map.name+"消息间隔:$timeInterval  ms")
            }

            AGVCmd.stateinfo.name -> {
                Log.d(TAG, AGVCmd.stateinfo.name+"消息间隔:$timeInterval  ms")
            }

            AGVCmd.scan.name -> {
                Log.d(TAG, AGVCmd.scan.name+"消息间隔:$timeInterval  ms")
            }

            AGVCmd.reflectormap_result.name -> {
                Log.d(TAG, AGVCmd.reflectormap_result.name+"消息间隔:$timeInterval  ms")
            }
            AGVCmd.shelvestask.name->{

            }
            AGVCmd.doorControl.name->{

            }
            AGVCmd.arriveShelf.name->{

            }

            else->{
                Log.d(TAG, "其他消息间隔:$timeInterval  ms")
            }
        }
    }

    override fun onValidationFail(client: TcpClient, tcpMsg: TcpMsg) {

    }
}